package workshopdesign.Control;

import java.util.*;
import workshopdesign.Model.*;

public class DVDController {

    private static DVDController dvdControl;
    private HashMap<Long, DVD> dvd_list;
    private HashMap<Long, Copy> copy_list;
    private long next_dvd_id = 0;
    private long next_copy_id = 0;

    private DVDController() {
        dvd_list = new HashMap<Long, DVD>();
        copy_list = new HashMap<Long, Copy>();
    }

    public static DVDController getDVDControl() {
        if (dvdControl == null) {
            dvdControl = new DVDController();
        }
        return dvdControl;
    }

    public static HashMap<Long, DVD> save_dvd(){
        return new HashMap<Long, DVD>(getDVDControl().dvd_list);
    }

    public static long save_dvd_id(){
        return DVDController.getDVDControl().next_dvd_id;
    }

    public static void load_dvd(HashMap<Long, DVD> dvd_list, long next_dvd_id){
        getDVDControl().dvd_list = new HashMap<Long, DVD>(dvd_list);
        getDVDControl().next_dvd_id = next_dvd_id;
    }

    public static HashMap<Long, Copy> save_copy(){
        return new HashMap<Long, Copy>(getDVDControl().copy_list);
    }

    public static long save_copy_id(){
        return getDVDControl().next_copy_id;
    }

    public static void load_copy(HashMap<Long, Copy> copy_list, long next_copy_id){
        getDVDControl().copy_list = new HashMap<Long, Copy>(copy_list);
        getDVDControl().next_copy_id = next_copy_id;
    }

    public void add_dvd(String title, String director, Date publicationDate) {
        next_dvd_id++;
        DVD dvd = new DVD(next_dvd_id, title, director, publicationDate);
        dvd_list.put(next_dvd_id, dvd);
    }

    public void update_dvd(long dvd_id, String title, String director, Date publicationDate) {
        DVD dvd = dvd_list.get(dvd_id);
        dvd.setTitle(title);
        dvd.setDirector(director);
        dvd.setPublicationDate(publicationDate);
    }

    public void remove_dvd(long dvd_id) {
        Collection<Copy> copies = get_Copy_by_DVDId(dvd_id);
        for(Copy copy : copies){
            remove_copy(copy.getId());
        }
        dvd_list.remove(dvd_id);
    }

    public Collection<DVD> get_dvd() {
        return dvd_list.values();
    }

    public DVD get_dvd(long dvd_id) {
        DVD dvd = dvd_list.get(dvd_id);
        return dvd;
    }

    public void add_copy(Date purchaseDate, long purchasePrice, long dvd_id) {
        next_copy_id++;
        Copy copy = new Copy(next_copy_id, purchaseDate, purchasePrice, dvd_id);
        copy_list.put(next_copy_id, copy);
    }

    public void update_copy(long copy_id, Date purchaseDate, long purchasePrice) {
        Copy copy = copy_list.get(copy_id);
        copy.setPurchaseDate(purchaseDate);
        copy.setPurchasePrice(purchasePrice);
    }

    public DVD get_DVD_by_CopyID(long copy_id) {
        Copy copy = copy_list.get(copy_id);
        long dvd_id = copy.getDVD_id();
        DVD dvd = dvd_list.get(dvd_id);
        return dvd;
    }

    public Collection<Copy> get_Copy_by_DVDId(long dvd_id){
        ArrayList<Copy> copies = new ArrayList<Copy>();

        for(Copy copy : copy_list.values()){
            if(copy.getDVD_id() == dvd_id){
                copies.add(copy);
            }
        }

        return copies;
    }

    public void remove_copy(long copy_id) {
        copy_list.remove(copy_id);
    }

    public Collection<Copy> getCopy() {
        return copy_list.values();
    }

    public Copy getCopy(long id) {
        Copy copy = copy_list.get(id);
        return copy;
    }
}
